﻿namespace dotnet_notes.Extensions
{
    /// <summary>
    /// 日期时间扩展类
    /// </summary>
    public static class DateTimeExtension
    {
        /// <summary>
        /// 获取可空 DateTime 的值(为 null 则抛出异常)
        /// </summary>
        public static DateTime GetValue(this DateTime? dateTime)
        {
            return dateTime ?? throw new ArgumentNullException(nameof(dateTime));
        }

        /// <summary>
        /// 转换成标准的日期字符串
        /// </summary>
        public static string ToDateString(this DateTime dateTime)
        {
            return dateTime.ToString(DateTimeConfig.Date);
        }

        /// <summary>
        /// 转换成标准的日期时间字符串
        /// </summary>
        public static string ToDateTimeString(this DateTime dateTime)
        {
            return dateTime.ToString(DateTimeConfig.DateTime);
        }

        /// <summary>
        /// 获取当天的开始时间
        /// </summary>
        public static DateTime GetStartTimeOfDay(this DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);
        }

        /// <summary>
        /// 获取当天的结束时间
        /// </summary>
        public static DateTime GetEndTimeOfDay(this DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 23, 59, 59);
        }
    }
}
